﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CustomsSystem.BLL;
using CustomsSystem.Model;
using Maticsoft.Common;

namespace WEB.T_bill
{
    public partial class sheetViewS : System.Web.UI.Page
    {
        public DataSet ds;
        
        protected void Page_Load(object sender, EventArgs e)
        { 
            this.tbTimestart.Attributes.Add("onClick", "new WdatePicker()");
            this.tbTimestop.Attributes.Add("onClick", "new WdatePicker()");
           
            if (!Page.IsPostBack)
            {
                 
                //默认时间为最近一月
                DateTime A = new DateTime();
                A = DateTime.Today;
                tbTimestop.Text = A.ToShortDateString();
                tbTimestart.Text = A.AddDays(-30).ToShortDateString();  
                Bind();  
            }
          
          
        }

        /// <summary>
        /// 跳转页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvSheetInfo_SelectedIndexChanging(object sender,GridViewSelectEventArgs e)
        {
            Session["sheetID"] = gvSheetInfo.Rows[e.NewSelectedIndex].Cells[0].Text.ToString ();
            Session["CustomerName"] = gvSheetInfo.Rows[e.NewSelectedIndex].Cells[4].Text.ToString ();
           
            int id = Convert.ToInt32(Request.QueryString["id"]);
            switch(id)
            {
                case 1: Response.Redirect("cost.aspx"); break;
                case 2: Response.Redirect("payment.aspx"); break;
                case 3: Response.Redirect("actualReceivables.aspx"); break;
                case 4: Response.Redirect("actualPayment.aspx"); break;
                default: return;
                    
            }
  
            
        }

        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            Bind();
           
        }
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void Bind()
        {
            SheetInfoManage sheetManage = new SheetInfoManage();
           
            string strQuery = ddlQuery.SelectedValue;
            string strSql = "  1=1 ";
            if (tbQuery.Text.Trim() != "")
            {
                if (ddlQuery.SelectedIndex == 8)
                {
                    if (PageValidate.IsNumber(tbQuery.Text.Trim()))
                    {
                        strSql += string.Format(" and {0} = '{1}'", strQuery, tbQuery.Text.Trim());
                    }
                    else { MessageBox.Show(this,"请输入数字"); }
                }
                else
                {
                    strSql += string.Format("and {0} like '%{1}%'", strQuery, tbQuery.Text.Trim());

                }
            }
            
            //日期搜索的判断
            if (tbTimestart.Text != "")
            {
                //日期截止时间为空的时候，默认为现在的时间
                if (tbTimestop.Text == "")
                {
                    DateTime timeStop = DateTime.Now;
                    strSql = strSql + " and agencyDate between '" + tbTimestart.Text.Trim() + "' and '" + timeStop + "'";
                }
                else
                {
                    strSql = strSql + " and agencyDate between '" + tbTimestart.Text.Trim() + "' and '" + tbTimestop.Text.Trim() + "'";
                }
            }
            strSql = strSql + " order by sheetID " + ddlorder.SelectedValue; ;
            DataSet ds = new DataSet();
            ds = sheetManage.GetViewOfUnaudit(strSql);
            if (ds.Tables[0].Rows.Count < 1)
            { lbj.Text = "在该段时间内没有数据"; }
            else
            { lbj.Text = ""; }
            gvSheetInfo.DataSource = ds;
            gvSheetInfo.DataBind(); 
        }

        /// <summary>
        /// 排序同样绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlorder_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind();
        }

      
        /// <summary>
        /// 允许分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvSheetInfo_PageIndexChanging1(object sender, GridViewPageEventArgs e)
        {
            //换页功能
            this.gvSheetInfo.PageIndex = e.NewPageIndex;
           //绑定GridView
            gvSheetInfo.DataBind();
            TextBox tb = (TextBox)gvSheetInfo.BottomPagerRow.FindControl("tbinPageNum");
            tb.Text = (gvSheetInfo.PageIndex + 1).ToString();
            Bind();
        }
       

      

        /// <summary>
        /// 跳页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                gvSheetInfo.DataBind();
                
                TextBox tb = (TextBox)gvSheetInfo.BottomPagerRow.FindControl("tbinPageNum");
                int num = Int32.Parse(tb.Text.Trim());

                GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
                gvSheetInfo_PageIndexChanging1(null, ea);
            }
            catch { MessageBox.Show(this, "请输入数字页码"); }
            
        }

        protected void gvSheetInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowIndex >= 0)
            //{
            //    BillManage bm = new BillManage();

            //    int sheetID = Convert.ToInt32(e.Row.Cells[0].Text);//获取sheetid
            //    DataSet ds = new DataSet();
            //    if (bm.ExistsSheetid(sheetID) == true)
            //    {
            //        e.Row.BackColor = System.Drawing.Color.SkyBlue;//显示颜色，可自定义
            //    }
            //}
            if (e.Row.RowIndex >= 0)
            {
                BillManage billBLL = new BillManage();
                DataSet dsLuRu = new DataSet();
                DataSet dsXiaoZhang = new DataSet();

                int sheetID = Convert.ToInt32(e.Row.Cells[0].Text);//获取sheetid
                dsXiaoZhang = billBLL.GetbillTypeId(sheetID);
                dsLuRu = billBLL.GetbillTypeId2(sheetID);
                if (dsXiaoZhang.Tables[0].Rows.Count == 2)//全部销账的委托单不显示
                {
                    e.Row.Visible = false;
                }
                if (dsLuRu.Tables[0].Rows.Count == 2)
                {
                    e.Row.BackColor = System.Drawing.Color.SkyBlue;//全部录入显示颜色，可自定义(蓝色)
                }
                else
                {
                    if (dsLuRu.Tables[0].Rows.Count ==1)
                    {
                        if (int.Parse(dsLuRu.Tables[0].Rows[0][0].ToString()) == 1)
                        {
                            e.Row.BackColor = System.Drawing.Color.Yellow;//应收显示颜色，可自定义(黄色)
                        }
                        if (int.Parse(dsLuRu.Tables[0].Rows[0][0].ToString()) == 2)
                        {
                            e.Row.BackColor = System.Drawing.Color.Green;//应付显示颜色，可自定义(绿色)
                        }
                    }
                }
            }


        }



    }
}